//
//  TSHandleAlertView.m
//  OCBase
//
//  Created by Duncan on 2021/1/12.
//

#import "TSHandleAlertView.h"

@interface TSHandleAlertView ()

@property (nonatomic, copy) AlertViewBlock block;

@end

@implementation TSHandleAlertView

#pragma mark ------- UIAlertControllerStyleAlert -------

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title {
    [self showSystemAlertViewMessage:message title:title okTitle:@"确定" cancelTitle:nil OKHandler:nil cancelHandler:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message OKHandler:(void (^ __nullable)(UIAlertAction *action))okHandler {
    [self showSystemAlertViewMessage:message title:nil okTitle:@"确定" cancelTitle:@"取消" OKHandler:okHandler cancelHandler:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title cancelTitle:(NSString *)cancelTitle {
    [self showSystemAlertViewMessage:message title:title okTitle:@"确定" cancelTitle:cancelTitle OKHandler:nil cancelHandler:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title cancelTitle:(NSString *)cancelTitle OKHandler:(void (^ __nullable)(UIAlertAction *action))okHandler {
    [self showSystemAlertViewMessage:message title:title okTitle:@"确定" cancelTitle:cancelTitle OKHandler:okHandler cancelHandler:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title okTitle:(NSString *)okTitle cancelTitle:(NSString *)cancelTitle OKHandler:(void (^ __nullable)(UIAlertAction *action))okHandler {
    [self showSystemAlertViewMessage:message title:title okTitle:okTitle cancelTitle:cancelTitle OKHandler:okHandler cancelHandler:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title okTitle:(NSString *)okTitle cancelTitle:(NSString *)cancelTitle OKHandler:(void (^ __nullable)(UIAlertAction *action))okHandler cancelHandler:(void (^ __nullable)(UIAlertAction *action))cancelHandler {
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title ?: @"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
        [cancelAction setValue:k_Color_999999 forKey:@"titleTextColor"];
        [alertController addAction:cancelAction];
    }
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle ?: @"确定" style:UIAlertActionStyleDefault handler:okHandler];
    [okAction setValue:k_Color_themeColor forKey:@"titleTextColor"];
    [alertController addAction:okAction];
    
    [[UIApplication currentRootTopViewController] presentViewController:alertController animated:YES completion:nil];
}

+ (void)showSystemAlertViewMessage:(NSString *)message title:(NSString *)title okTitle:(NSString *)okTitle cancelTitle:(NSString *)cancelTitle OKHandler:(void (^ __nullable)(UIAlertAction *action))okHandler cancelHandler:(void (^ __nullable)(UIAlertAction *action))cancelHandler addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
        [cancelAction setValue:k_Color_999999 forKey:@"titleTextColor"];
        [alertController addAction:cancelAction];
    }
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:okHandler];
    [okAction setValue:k_Color_themeColor forKey:@"titleTextColor"];
    [alertController addAction:okAction];
    [alertController addTextFieldWithConfigurationHandler:configurationHandler];
    [[UIApplication currentRootTopViewController] presentViewController:alertController animated:YES completion:nil];
}

#pragma mark ------- UIAlertControllerStyleActionSheet -------
+ (void)showSystemSheetViewWithTitleArray:(NSArray *)titleArray confirm:(AlertViewBlock)confirm {
    
    UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [cancelBtn setValue:k_Color_666666 forKey:@"titleTextColor"];
    [sheetController addAction:cancelBtn];
    
    if (titleArray.count > 0) {
        for (int i = 0; i < titleArray.count; i++) {
            UIAlertAction *action = [UIAlertAction actionWithTitle:titleArray[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (confirm) {
                    confirm(i);
                }
            }];
            [action setValue:k_Color_333333 forKey:@"titleTextColor"];
            [sheetController addAction:action];
        }
    }
    [[UIApplication currentRootTopViewController] presentViewController:sheetController animated:YES completion:nil];
}

+ (void)pad_showSystemSheetViewWithTitleArray:(NSArray * _Nullable)titleArray confirm:(AlertViewBlock _Nullable)confirm view:(UIView *)view frame:(CGRect)frame {
    UIAlertController *sheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [cancelBtn setValue:k_Color_333333 forKey:@"titleTextColor"];
    [sheetController addAction:cancelBtn];
    
    if (titleArray.count > 0) {
        for (int i = 0; i < titleArray.count; i++) {
            NSString *nameTitle = titleArray[i];
            UIAlertAction *action = [UIAlertAction actionWithTitle:nameTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (confirm) {
                    confirm(i);
                }
            }];
            
            if ([nameTitle isEqualToString:@"删除"]) {
//                [action setValue:UIColor.redColor forKey:@"titleTextColor"];
            } else {
//                [action setValue:k_Color_themeColor forKey:@"titleTextColor"];
            }
            [sheetController addAction:action];
        }
    }
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        
    } else {
        sheetController.popoverPresentationController.sourceView = view;
        sheetController.popoverPresentationController.sourceRect = frame;
    }
    
    [[UIApplication currentRootTopViewController] presentViewController:sheetController animated:YES completion:nil];
}

@end
